package cn.gem.rainbow.cfg.restful;

import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

/**
 * restful风格的404,
 * 返回json {"code":404,"msg":"访问路径不存在","data":null,"timestamp":1600536809148}
 * 而不是显示404.html
 * 在前后端分离开发的大背景下，是有需求的
 */
@RestController
public class RestNotFoundController implements ErrorController {
    /**
     * The return value from this method is not used; the property `server.error.path`
     * must be set to override the default error page path.
     *
     * @return the error path
     * @deprecated since 2.3.0 in favor of setting the property `server.error.path`
     */
    @Override
    public String getErrorPath() {
        return null;
    }


    private static final int NOT_FOUND_CODE = 404;

    private static final String ERROR_PATH = "/error";

    @RequestMapping(ERROR_PATH)
    @ResponseStatus(value= HttpStatus.NOT_FOUND)
    public ResultVO<?> handleError() {
        ResultVO<?> result = new ResultVO<>();
        result.setCode(NOT_FOUND_CODE);
        result.setMsg("访问路径不存在");
        result.setTimestamp(System.currentTimeMillis());
        return result;
    }
}
